package su2010.puz.extractors;

import java.awt.image.BufferedImage;

import su2010.puz.Extractor;
import su2010.puz.Filter;
import su2010.puz.ImageHelper;
import su2010.puz.util.ImageCls;
import su2010.puz.util.PixelExtractor;

/**
 * Implementation of feature extractor which uses ratio of number of dark pixels
 * and number of all pixels in image, as a feature.
 *
 */
public class PixelIntervalExtractor implements Extractor {

	private BufferedImage image;
	private int interval_begin;
	private int interval_end;
	private int color_mode;
	private int numOutput = 1;
	private Filter[] filters = null;
	private String name;
	
	public PixelIntervalExtractor() {
		this.interval_begin = 0;
		this.interval_end = 0;
		this.color_mode = ImageHelper.RED;
		this.name = "";
	}
	
	public PixelIntervalExtractor(String name) {
		this.interval_begin = 0;
		this.interval_end = 0;
		this.color_mode = ImageHelper.RED;
		this.name = name;
	}

	public void setInterval(int interval_begin, int interval_end) {
		this.interval_begin = interval_begin;
		this.interval_end = interval_end;
	}
	
	public void setColorMode(int mode) {
		this.color_mode = mode;
	}

	public void setFilter(Filter ... filters){
		this.filters = filters;
	}
	
	@Override
	public int getNumOutput() {
		return numOutput;
	}

	@Override
	public void setImage(ImageCls image) {
		this.image = image.getImage();	
	}

	public void setImage(BufferedImage image){
		this.image = image;
	}
	@Override
	public float[] extract() {
		if(filters!=null)
			for(Filter filter : filters)
				image = filter.apply(image);
		//ImageHelper.showImage(image, "bla");
		int in_interval = 0;
		int[][][] pixels = PixelExtractor.getRGBL(image);
		int height = image.getHeight();
		int width = image.getWidth();
		int all = width*height;
		for(int i = 0; i< width;i++)
			for(int j = 0; j<height;j++)
				if(	pixels[j][i][color_mode] >= interval_begin &&
					pixels[j][i][color_mode] <= interval_end)
						in_interval++;
		float[] ratio = new float[]{(float)in_interval/all};
		return ratio;
	}

	@Override
	public String getName() {
		return name;
	}

}
